#coding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header

class myMailSender:
    def __init__(self,myaddr,smtpserver,myuser,mypasswd,usingssl=False):
        self.m_myaddr=myaddr
        self.m_smtpserver=smtpserver
        self.m_myuser=myuser
        self.m_mypasswd=mypasswd
        if usingssl:
            self.m_smtp = smtplib.SMTP_SSL()
        else:
            self.m_smtp = smtplib.SMTP()

    def connect(self):
        self.m_smtp.connect(self.m_smtpserver)
        self.m_smtp.login(self.m_myuser,self.m_mypasswd)
    
    def disconnect(self):
        self.m_smtp.quit()

    def sendMail(self,strTitle,strHtmlContent,toaddr):
        msg = MIMEText(strHtmlContent,'html','utf-8')
        msg['Subject'] = Header(strTitle, 'utf-8')
        self.m_smtp.sendmail(self.m_myaddr, toaddr, msg.as_string())

if __name__ == '__main__':
    mailsender=myMailSender("598067535@qq.com","smtp.qq.com","598067535","",True)
    mailsender.connect()
    mailsender.sendMail("hello","this is a test","598067535@qq.com")
    mailsender.disconnect()
